#! /bin/sh

### BEGIN INIT INFO
# Provides:          spawn-fcgi-munin-html
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       starts FastCGI for Munin-Html
### END INIT INFO
# --------------------------------------------------------------
# Munin-CGI-HTML Spawn-FCGI Startscript by Julien Schmidt
# eMail: munin-trac at julienschmidt.com
# www:   http://www.julienschmidt.com
# --------------------------------------------------------------
# Install: 
#   1. Copy this file to /etc/init.d
#   2. Edit the variables below
#   3. run "update-rc.d spawn-fcgi-munin-html defaults"
# --------------------------------------------------------------
# Last Update: 13. November 2012
#
# Please change the following variables:

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=spawn-fcgi-munin-html
PID_FILE=/var/run/munin/fcgi-html.pid
SOCK_FILE=/var/run/munin/fcgi-html.sock
DAEMON=/usr/bin/spawn-fcgi
DAEMON_OPTS="-s $SOCK_FILE -U nginx -u nginx -g www-data /usr/lib/cgi-bin/munin-cgi-html -P $PID_FILE"

# --------------------------------------------------------------
# No edits necessary beyond this line
# --------------------------------------------------------------

if [ ! -x $DAEMON ]; then
	echo "File not found or is not executable: $DAEMON!"
	exit 0
fi

status() {
	if [ ! -r $PID_FILE ]; then
		return 1
	fi
	
	FCGI_PID=`cat $PID_FILE`
	if [ -z "${FCGI_PID}" ]; then
		return 1
	fi
	
	FCGI_RUNNING=`ps -p ${FCGI_PID} | grep ${FCGI_PID}`
	if [ -z "${FCGI_RUNNING}" ]; then
		return 1
	fi
	
	return 0
}
	
start() {
	if status; then
		echo "FCGI is already running!"
		exit 1
	else
		$DAEMON $DAEMON_OPTS
	fi
}

stop () {	
	if ! status; then
		echo "No PID-file at $PID_FILE found or PID not valid. Maybe not running"
		exit 1
	fi
	
	# Kill process
	kill -9 `cat $PID_FILE`
	
	# Remove PID-file
	rm -f $PID_FILE
	
	# Remove Sock-File
	rm -f $SOCK_FILE
}

case "$1" in
	start)
		echo "Starting $NAME: "
		start
		echo "... DONE"
	;;

	stop)
		echo "Stopping $NAME: "
		stop
		echo "... DONE"
	;;

	force-reload|restart)
		echo "Stopping $NAME: "
		stop
		echo "Starting $NAME: "
		start
		echo "... DONE"
	;;
	
	status)
		if status; then
			echo "FCGI is RUNNING"
		else
			echo "FCGI is NOT RUNNING"
		fi
	;;
	
	*)
		echo "Usage: $0 {start|stop|restart|status}"
		exit 1
		;;
esac

exit 0